package librarysys;

import java.util.Scanner;

public class Library {
    Scanner sc = new Scanner(System.in);
    private static Book[] books;
    // 初始化所有图书
    static {
        books = new Book[10];
        for (int i = 0; i < 10; i++) {
            Book book = new Book("图书"+ (i+1),"作者"+ (i+1));
            books[i] = book;
        }
    }
    // 陈列图书
    public void displayBooks() {
        System.out.println("编号    图书名称    图书作者    状态");
        for (int i = 0; i < 10; i++) {
            System.out.print((i+1)+"    ");
            System.out.print("《"+books[i].getTitle()+"》    ");
            System.out.print(books[i].getAuthor()+"    ");
            String isBorrowed = books[i].isBorrowed() ? "已被借走" : "未被借走";
            System.out.print(isBorrowed);
            System.out.println();
        }
    }

    // 借阅图书
    public void borrowBook() {
        System.out.print("请输入图书名称：");
        String title = sc.nextLine();
        for (int i = 0; i < 10; i++) {
            if (books[i].getTitle().equals(title)) {
                if (!books[i].isBorrowed()) {
                    books[i].setBorrowed(true);
                    System.out.println("成功借阅图书：《"+title+"》");
                }else {
                    System.out.println("图书《"+title+"》已被借走");
                }
                return;
            }
        }
        System.out.println("馆内没有图书《"+title+"》");
    }

    // 归还图书
    public void returnBook() {
        System.out.print("请输入图书名称:");
        String title = sc.nextLine();
        for (int i = 0; i < 10; i++) {
            if (books[i].getTitle().equals(title)) {
                if (books[i].isBorrowed()) {
                    books[i].setBorrowed(false);
                    System.out.println("成功归还图书：《"+title+"》");
                }else {
                    System.out.println("您未借阅图书《"+title+"》");
                }
                return;
            }
        }
        System.out.println("馆内没有图书《"+title+"》");
    }
}
